//
//  XPBaseCollectionViewController.swift
//  PPKit
//
//  Created by xiaopin on 2019/6/21.
//  Copyright © 2019 PPKit. All rights reserved.
//  https://gitee.com/xiaopindev/PPKit
//  https://github.com/xiaopindev
//  https://xiaopin.cnblogs.com

import UIKit
import MJRefresh

/// 重用标志，如果使用sb和xib的cell，也设置相同如下的值:"reuseCell"
private let reuseIdentifier = "ReuseItemCell"

protocol XPBaseCollectionViewControllerDataSource {
    func collectionViewController(_ collectionViewController:XPBaseCollectionViewController, layoutForCollectionView:UICollectionView) -> UICollectionViewLayout
}

class XPBaseCollectionViewController: XPBaseViewController {
    //MARK: 属性
    //分页属性
    var pageIndex:Int = 1
    var pageSize:Int = 20
    var maxIndex:Int = 0
    
    //cell动画
    var animationType:Int = 0
    var lastCellIndex:Int = 0
    
    /// 滚动条上次记录的Offset
    var prevOffset:CGPoint = CGPoint(x: 0, y: 0)
        
    /// 表格数据源
//    var dataSource = [Any]()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
        collectionView.backgroundColor = pp_color(.CFFFFFF)
        let myLayout = collectionViewController(self, layoutForCollectionView: collectionView)
        collectionView.collectionViewLayout = myLayout
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.alwaysBounceVertical = true
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        }else{
            automaticallyAdjustsScrollViewInsets = false
        }
        collectionView.beAddInto(view).snp.makeConstraints({ (m) in
            m.edges.equalToSuperview()
        })
        return collectionView
    }()
    
    //MARK: 系统基类方法
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func initSubViews() {
        super.initSubViews()
        
        //        if #available(iOS 11.0, *){
        //            clistView.contentInsetAdjustmentBehavior = .never
        //        }
    }
    
}

//MARK: #代理方法#
extension XPBaseCollectionViewController:XPBaseCollectionViewControllerDataSource,UICollectionViewDelegate,UICollectionViewDataSource,CHTCollectionViewDelegateWaterfallLayout {
    
    func collectionViewController(_ collectionViewController: XPBaseCollectionViewController, layoutForCollectionView: UICollectionView) -> UICollectionViewLayout {
        let layout = CHTCollectionViewWaterfallLayout()
        layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
        layout.minimumColumnSpacing = 15
        layout.minimumInteritemSpacing = 15
        return layout
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        cell.backgroundColor = .red
        return cell
    }
    
    //MARK: - CHTCollectionViewDelegateWaterfallLayout
    func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAt indexPath: IndexPath!) -> CGSize {
        return CGSize(width: pp_screen_width / 3, height: pp_screen_width / 3)
    }

    func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, columnCountForSection section: Int) -> Int {
        return 3
    }

    func collectionView(_ collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }
}


//MARK: #重写基类方法#
extension XPBaseCollectionViewController{
    func loadNewData() {
        
    }
    
    func loadMoreData() {
        
    }
    
    /**
     下拉刷新
     */
    func addPullDownAction() {
        let header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(loadNewData))
        header.setTitle("MT_Refresh_Idle".localized, for: .idle)
        header.setTitle("MT_Refresh_Pulling".localized, for: .pulling)
        header.setTitle("MT_Refresh_Refreshing".localized, for: .refreshing)
        header.lastUpdatedTimeLabel?.isHidden = false
        header.stateLabel?.isHidden = false
        self.collectionView.mj_header = header
    }
    
    /**
     上拉加载更多
     */
    func addPullUpAction(){
        let footer = XPAutoFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        
        self.collectionView.mj_footer = footer;
    }
}
